﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using yq.ControllerBase;
using yq.WebModel;

namespace yq.Controller.Sys
{
    /// <summary>
    /// 这个系统用不到定时任务
    /// </summary>
    public class TaskController : BaseController
    {
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            var list = yq.GetService.Create<yq.IService.ISys>().SelectTaskAllList();
            ListPage<Model.Logic.SysTask> listPage = new ListPage<Model.Logic.SysTask>();
            listPage.List = list;
            listPage.AddRoleLinkButton("查看", "ViewTask", "Task");
            listPage.AddRoleLinkButton("编辑", "EditTask", "Task");
            listPage.AddRoleLinkButton("添加", "AddTask", "Task");
            listPage.AddRoleLinkButton("删除", "DelTask", "Task");
            listPage.AddRoleLinkButton("执行", "ExcTask", "Task");


            return View(listPage);
        }

        /// <summary>
        /// 查看
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("RowId")]
        public ActionResult ViewTask()
        {
            Model.Logic.SysTask task = GetModel<Model.Logic.SysTask>();
            return PartialView(task);
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <returns></returns>
        public ActionResult AddTask()
        {
            Model.Logic.SysTask task = new Model.Logic.SysTask()
            {
                StartTime = DateTime.Now
            };
            return PartialView(task);
        }

        /// <summary>
        /// 编辑任务
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("RowId")]
        public ActionResult EditTask()
        {
            Model.Logic.SysTask task = GetModel<Model.Logic.SysTask>();

            return PartialView(task);
        }

        /// <summary>
        /// 添加任务
        /// </summary>
        /// <returns></returns>
        [ErrorFilter("添加任务", ErrorFilterType.ReturnValue)]
        public ActionResult DoAddTask()
        {
            yq.Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
            Model.Logic.SysTask task = new Model.Logic.SysTask();
            TryUpdateModel(task);

            if (verify(task))
            {
                rv = yq.GetService.Create<yq.IService.ISys>().AddTask(task);
            }
            else
            {
                rv.False("格式不正确");
            }


            return Json(rv);
        }

        /// <summary>
        /// 编辑任务
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("RowId", yq.Controller.VerifyHelp.TaskTimeKey)]
        [ErrorFilter("编辑任务", ErrorFilterType.ReturnValue)]
        public ActionResult DoEditTask()
        {
            Model.Logic.SysTask task = GetModel<Model.Logic.SysTask>();
            TryUpdateModel(task);
            var rv = yq.GetService.Create<yq.IService.ISys>().EditTask(task);

            return Json(rv);
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [VerifyFilter("RowId")]
        public ActionResult DelTask()
        {
            Model.Logic.SysTask task = GetModel<Model.Logic.SysTask>();
            var rv = yq.GetService.Create<yq.IService.ISys>().DelTask(task.Rowid);

            return Json(rv);
        }

        /// <summary>
        /// 立即执行
        /// </summary>
        /// <param name="rowid"></param>
        /// <returns></returns>
        [VerifyFilter("RowId")]
        public ActionResult ExcTask(string rowid)
        {
            yq.Dictionary.ReturnValue<string> rv = new Dictionary.ReturnValue<string>();
            bool b = yq.GetService.Create<yq.IService.ITask>().RunWait(rowid);
            if (b)
            {
                rv.True(rowid);
            }
            else
            {
                rv.False("执行失败");
            }
            return Json(rv);
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool verify(yq.Model.Logic.SysTask model)
        {
            string temp = "";
            string interval = model.Interval;
            try
            {
                if (model.Types != Enums.SysTask.Types.ForTask)
                {
                    if (model.Types == Enums.SysTask.Types.Year)
                    {
                        temp = "MMddHHmmss";
                    }
                    else if (model.Types == Enums.SysTask.Types.Mon)
                    {
                        temp = "ddHHmmss";
                    }
                    else if (model.Types == Enums.SysTask.Types.Day)
                    {
                        temp = "HHmmss";
                    }
                    DateTime dt = new DateTime();

                    if (DateTime.TryParseExact(interval, temp, null, System.Globalization.DateTimeStyles.None, out dt) == false)
                    {
                        return false;
                    }
                }
                else
                {
                    int i = 0;
                    if (int.TryParse(interval, out i) == false)
                    {
                        return false;
                    }

                    if (i < 3)
                    {
                        return false;
                    }

                }

            }
            catch
            {
                return false;
            }
            return true;
        }

    }
}
